package com.mall.service.mall;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.mall.MallActivityService;
import com.mall.custom.dao.read.MallActivityReadMapper;
import com.mall.custom.dao.write.MallActivityWriteMapper;
import com.mall.pojo.MallActivity;
import com.mall.pojo.MallActivityExample;
import com.mall.pojo.MallActivityExample.Criteria;
import com.mall.query.MallActivityQuery;
import com.mall.query.Page;
import com.mall.searchdo.SearchResultIdAndCountDO;

@Service
public class MallActivityServiceImpl implements MallActivityService {

	@Autowired
	private MallActivityReadMapper mallActivityReadMapper;

	@Autowired
	private MallActivityWriteMapper mallActivityWriteMapper;

	@Override
	public List<MallActivity> getMallActivityList(MallActivityQuery mallActivityQuery, Page page) {
		if (page == null) {
			page = Page.getInitPage();
		}
		MallActivityExample mallActivityExample = new MallActivityExample();
		mallActivityExample.setLimit(page.getLength());
		mallActivityExample.setStart(page.getBegin());
		if (StringUtils.isNotBlank(mallActivityQuery.getOrderBy())) {
			mallActivityExample.setOrderByClause(mallActivityQuery.getOrderBy());
		}
		Criteria criteria = mallActivityExample.createCriteria();
		if (mallActivityQuery.getMallId() != null && mallActivityQuery.getMallId() > 0) {
			criteria.andMallIdEqualTo(mallActivityQuery.getMallId());
		}

		if (mallActivityQuery.getCommkey() != null && mallActivityQuery.getCommkey() > 0) {
			criteria.andCommOnlyKeyEqualTo(mallActivityQuery.getCommkey());
		}

		if (mallActivityQuery.getId() != null && mallActivityQuery.getId() > 0) {
			criteria.andIdEqualTo(mallActivityQuery.getId());
		}

		if (mallActivityQuery.getIds() != null && mallActivityQuery.getIds().size() > 0) {
			criteria.andIdIn(mallActivityQuery.getIds());
		}

		if (mallActivityQuery.getStatus() != null) {
			criteria.andStatusEqualTo(mallActivityQuery.getStatus());
		}

		if (mallActivityQuery.getType() != null) {
			criteria.andTypeEqualTo(mallActivityQuery.getType());
		}

		if (mallActivityQuery.getEffective() != null && mallActivityQuery.getEffective() == true) {
			criteria.andActivityStartTimeLessThanOrEqualTo(new Date());
			criteria.andActivityEndTimeGreaterThanOrEqualTo(new Date());
		}
		if (mallActivityQuery.getSuitPerson() != null) {
			criteria.andSuitPersonLessThanOrEqualTo(mallActivityQuery.getSuitPerson());
		}
		if (mallActivityQuery.getActivityRull() != null) {
			criteria.andActivityRullLike("%"+mallActivityQuery.getActivityRull()+"%");
		}
		return mallActivityReadMapper.selectByExample(mallActivityExample);

	}

	@Override
	public int updateMallActivity(MallActivity mallActivity, Long id) {
		mallActivity.setId(id);
		mallActivity.setGmtModify(new Date());
		return this.mallActivityWriteMapper.updateByPrimaryKeySelective(mallActivity);
	}

	@Override
	public int insertMallActivity(MallActivity mallActivity) {
		mallActivity.setGmtCreate(new Date());
		mallActivity.setGmtModify(new Date());
		return this.mallActivityWriteMapper.insertSelective(mallActivity);
	}

	@Override
	public List<MallActivity> getMallActivityType1List4ListByMallIds(List<Long> mallIds) {
		List<SearchResultIdAndCountDO> searchResultIdAndCountDOList = this.mallActivityReadMapper
				.getMallActivityType1List4ListByMallIds(mallIds);
		if (searchResultIdAndCountDOList == null || searchResultIdAndCountDOList.size() <= 0) {
			return null;
		}
		List<Long> idList = new ArrayList<>();
		for (SearchResultIdAndCountDO searchResultIdAndCountDO : searchResultIdAndCountDOList) {
			idList.add(searchResultIdAndCountDO.getId());
		}
		MallActivityQuery mallActivityQuery = new MallActivityQuery();
		mallActivityQuery.setIds(idList);
		return this.getMallActivityType1List4ListByMallIds(mallIds);
	}

	@Override
	public List<MallActivity> getMallActivityType2List4ListByMallIds(List<Long> mallIds) {
		List<SearchResultIdAndCountDO> searchResultIdAndCountDOList = this.mallActivityReadMapper
				.getMallActivityType2List4ListByMallIds(mallIds);
		if (searchResultIdAndCountDOList == null || searchResultIdAndCountDOList.size() <= 0) {
			return null;
		}
		List<Long> idList = new ArrayList<>();
		for (SearchResultIdAndCountDO searchResultIdAndCountDO : searchResultIdAndCountDOList) {
			idList.add(searchResultIdAndCountDO.getId());
		}
		MallActivityQuery mallActivityQuery = new MallActivityQuery();
		mallActivityQuery.setIds(idList);
		return this.getMallActivityList(mallActivityQuery, Page.getInitPage(searchResultIdAndCountDOList.size()));
	}

	@Override
	public MallActivity getMallActivityById(Long mallActivityId) {
		return this.mallActivityReadMapper.selectByPrimaryKey(mallActivityId);
	}

	@Override
	public List<MallActivity> getMallActivityListByLevel(Integer level,Integer onlyKey) {
		return this.mallActivityReadMapper.getMallActivityListByLevel(level,onlyKey);
	}

}
